# Contributor: Marian Buschsieweke <marian.buschsieweke@posteo.net>
# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
pkgname=py3-litex-hub-modules
_pkgname=litex
pkgver=2023.12
pkgrel=4
pkgdesc="Python modules containing verilog and/or data files for use with LiteX"
url="https://github.com/litex-hub"
license="BSD-2-Clause"
arch="all"
# match this commit against https://github.com/litex-hub/pythondata-software-picolibc/tree/master/pythondata_software_picolibc
_picolibc_commit="f165dc22f1f67e3e8bdc8edf750ff7dc596de2ff"
_fpnew_commit="855bb82b6e85772fc290fa8b9c14fdd8f1b16be7"
_riscv_dbg_commit="6d38d957b036231db668666255e938c91b7ce424"
_trace_debugger_commit="0aafa398e208ad79826407e3805642987287cfae"
_common_cells_commit="790f2385c01c83022474eede55809666209216e3"
_fpu_div_sqrt_mvp_commit="83a601f97934ed5e06d737b9c80d98b08867c5fa"
_modules="
	pythondata-cpu-blackparrot:2023.12:BSD-3-Clause
	pythondata-cpu-cv32e40p:2023.12:Apache-2.0
	pythondata-cpu-cv32e41p:2023.12:Apache-2.0
	pythondata-cpu-cva5:2023.12:Apache-2.0
	pythondata-cpu-cva6:2023.12:Apache-2.0
	pythondata-cpu-ibex:2023.12:Apache-2.0
	pythondata-cpu-lm32:2020.04:EPL-1.0
	pythondata-cpu-marocchino:2023.12:MPL-2.0
	pythondata-cpu-microwatt:2023.12:CC-BY-SA-4.0
	pythondata-cpu-minerva:2023.12:BSD-2-Clause
	pythondata-cpu-mor1kx:2020.04:MPL-2.0
	pythondata-cpu-naxriscv:2023.12:MIT
	pythondata-cpu-picorv32:2020.04:ISC
	pythondata-cpu-rocket:2020.04:Apache-2.0
	pythondata-cpu-serv:2020.04:ISC
	pythondata-cpu-vexriscv:2020.04:MIT
	pythondata-cpu-vexriscv_smp:2023.12:MIT
	pythondata-misc-tapcfg:2020.04:LGPL-2.1-only
	pythondata-software-compiler_rt:2020.04:Apache-2.0
	pythondata-software-picolibc:2023.12:BSD-3-Clause
	valentyusb:2023.12:BSD-3-Clause
	"
depends="
	python3
	"
makedepends="
	py3-setuptools
	py3-gpep517
	py3-wheel
	py3-installer
	"
subpackages="$pkgname-pyc"
source="
	picolibc-$_picolibc_commit.tar.gz::https://github.com/picolibc/picolibc/archive/$_picolibc_commit.tar.gz
	fpnew-$_fpnew_commit.tar.gz::https://github.com/antmicro/fpnew/archive/$_fpnew_commit.tar.gz
	riscv-dbg-$_riscv_dbg_commit.tar.gz::https://github.com/antmicro/riscv-dbg/archive/$_riscv_dbg_commit.tar.gz
	trace_debugger-$_trace_debugger_commit.tar.gz::https://github.com/antmicro/trace_debugger/archive/$_trace_debugger_commit.tar.gz
	common_cells-$_common_cells_commit.tar.gz::https://github.com/pulp-platform/common_cells/archive/$_common_cells_commit.tar.gz
	fpu_div_sqrt_mvp-$_fpu_div_sqrt_mvp_commit.tar.gz::https://github.com/pulp-platform/fpu_div_sqrt_mvp/archive/$_fpu_div_sqrt_mvp_commit.tar.gz

	0001-fix-pythondata_software_compiler_rt-python3.patch
	0002-fix-pythondata-cpu-blackparrot-python3.patch
	"
builddir="$srcdir"
# most LiteX-Hub repos contain no unit tests. The basically wrap upstream
# repos in a fashion suitable for use with LiteX, so they can rely on unit
# tests of the upstream repos. Those LiteX-Hub repos that do have tests
# depend on LiteX to be installed, but LiteX depends on these for its unit
# tests (circular dependency). The unit tests in LiteX are more important, so
# we disable tests here.
options="!check"

# The data contains ELF files intended for the softcore CPUs, which cannot
# be stripped with the native toolchain due to mismatching target architecture.
options="$options !strip"

_repo_name() {
	echo ${1%%:*}
}

_pkgver() {
	a=${1%:*}
	echo ${a#*:}
}

_version_by_name() {
	for _name_ver_license in $_modules; do
		if [ "$1" = "$(_repo_name "$_name_ver_license")" ]; then
			_pkgver "$_name_ver_license"
		fi
	done
}

_supply_git_submodule() {
	rmdir "$1" || rm "$1"
	ln -s "$2" "$1"
}

_license() {
	echo $1 | cut -d : -f 3
}

for _name_ver_license in $_modules; do
	_name="$(_repo_name "$_name_ver_license")"
	_ver="$(_pkgver "$_name_ver_license")"
	source="$source $_name-$_ver.tar.gz::https://github.com/litex-hub/$_name/archive/refs/tags/$_ver.tar.gz"
	subpackages="$subpackages py3-litex-hub-$_name:_subpkg"
done

prepare() {
	local _name
	local _ver
	default_prepare

	# remove drivers for OSX, Solaris and Windows as well as tools for mingw.
	# We don't need them and they cause lots of warnings
	rm -rf "$srcdir"/pythondata-misc-tapcfg-2020.04/pythondata_misc_tapcfg/data/drivers
	rm -rf "$srcdir"/pythondata-misc-tapcfg-2020.04/pythondata_misc_tapcfg/data/scons-tools

	# setuptools.find_packages() doesn't work here and many instance of
	#     Warning: Package '<data_pkg_name>' is absent from the packages configuration.
	# are triggered. Using setuptools.find_namespace_packages() instead does
	# the trick.
	find "$srcdir" -maxdepth 2 -name setup.py -exec \
		sed \
			-e 's/packages=setuptools\.find_packages/packages=setuptools\.find_namespace_packages/g' \
			-i {} \;

	# manually supply pythondata-software-picolibc git submodule from downloaded source
	_name=pythondata-software-picolibc
	_ver="$(_version_by_name "$_name")"
	_supply_git_submodule \
		"$srcdir/$_name-$_ver/pythondata_software_picolibc/data" \
		"$srcdir/picolibc-$_picolibc_commit"

	# manually supply pythondata-cpu-cv32e40p git submodule from downloaded source
	_name=pythondata-cpu-cv32e40p
	_ver="$(_version_by_name "$_name")"
	_supply_git_submodule \
		"$srcdir/$_name-$_ver/pythondata_cpu_cv32e40p/system_verilog/rtl/fpnew" \
		"$srcdir/fpnew-$_fpnew_commit"
	_supply_git_submodule \
		"$srcdir/$_name-$_ver/pythondata_cpu_cv32e40p/system_verilog/rtl/riscv-dbg" \
		"$srcdir/riscv-dbg-$_riscv_dbg_commit"
	_supply_git_submodule \
		"$srcdir/$_name-$_ver/pythondata_cpu_cv32e40p/system_verilog/rtl/trace_debugger" \
		"$srcdir/trace_debugger-$_trace_debugger_commit"

	# manually supply git submodules for fpnew
	_supply_git_submodule \
		"$srcdir/fpnew-$_fpnew_commit/src/common_cells" \
		"$srcdir/common_cells-$_common_cells_commit"
	_supply_git_submodule \
		"$srcdir/fpnew-$_fpnew_commit/src/fpu_div_sqrt_mvp" \
		"$srcdir/fpu_div_sqrt_mvp-$_fpu_div_sqrt_mvp_commit"
}

build() {
	local _name
	local _version
	for _name_ver_license in $_modules; do
		_name="$(_repo_name "$_name_ver_license")"
		_version="$(_pkgver "$_name_ver_license")"
		echo "--> Building LiteX Hub module $_name"

		cd "$srcdir/$_name-$_version"
		gpep517 build-wheel \
			--wheel-dir "$srcdir/.dist" \
			--output-fd 3 3>&1 >&2
	done
}

package() {
	# This package will be empty after all the subpackage split functions are
	# executed. We use it as a meta-package to pull in all the subpackage for
	# anyone interested in a full LiteX installation.
	for _name_ver_license in $_modules; do
		_name="$(_repo_name "$_name_ver_license")"
		depends="$depends py3-litex-hub-$_name=$pkgver-r$pkgrel"
	done

	local wheel
	mkdir -p "$pkgdir"

	for wheel in "$srcdir/.dist"/*.whl; do
		python3 -m installer -d "$pkgdir" \
			"$wheel"
	done
}

_subpkg() {
	local _name
	local _pyname
	_name="${subpkgname#py3-litex-hub-}"
	_pyname="${_name//-/_}"
	amove usr/lib/python3*/site-packages/"$_pyname"
	amove usr/lib/python3*/site-packages/"$_pyname"-*.dist-info

	for _name_ver_license in $_modules; do
		if [ "$_name" = "$(_repo_name "$_name_ver_license")" ]; then
			license="$(_license "$_name_ver_license")"
			pkgdesc="Python module containing verilog/data files \"$_name\" for use with LiteX"
		fi
	done
}

sha512sums="
795a9983d1bc0f851d8f1116e0c580a893aefa82d2034796d14868a1ec14f2ec6d2a4f5bf649b073b8e59f903bd65642766580c52573c7064ba3fb6b972e6e07  picolibc-f165dc22f1f67e3e8bdc8edf750ff7dc596de2ff.tar.gz
66b088bda0a629af97ad109ba06a7d0f9da7f2fb515c6de9752862c9ff77db9c23e520c5bffbb98c3ecc6fb2f800ea266f8b8359d3ad3c3ea2c970490877d2e1  fpnew-855bb82b6e85772fc290fa8b9c14fdd8f1b16be7.tar.gz
534a1c2358c85ed7464971b6cd2f06d949ec1db918ed531a75e0b5fe6da9a1b86f9cb675f1fb400cec9c5d607ef40bc0320a6e38b71650f1d0f4111d1c41dd02  riscv-dbg-6d38d957b036231db668666255e938c91b7ce424.tar.gz
3979457f51be8d1b079feaa07e04b02b3db425e1e02a52ae46c1fd4d304c2b513cf49d180c6018fe01e85908523712ce069de2893f3d180019264de42f6c619d  trace_debugger-0aafa398e208ad79826407e3805642987287cfae.tar.gz
926856d4ba701a14c29bf4fe7d9cec774b95fe8778bb0029e1aa361e144a99f328cf4f0d5806a7dee21a2e152a662b9aa7e9bb45b84c816280e0b6ac943979fc  common_cells-790f2385c01c83022474eede55809666209216e3.tar.gz
d7bf78ddbb00239a37e6410994bfc75c07a295f44aa0ecf968949448433a05282c7279f5d46b77a12faa77bba2cac92251cb56f972a95b22443c6045feba49db  fpu_div_sqrt_mvp-83a601f97934ed5e06d737b9c80d98b08867c5fa.tar.gz
3e1f7e96fc144b1f819ffad5262d669e3abf214c89d547344f66b325a6c60b523f03b19a68ea2166fe98733cb310255a36ef7c843b30a3227517a46ac37d7c33  0001-fix-pythondata_software_compiler_rt-python3.patch
1df7abd5c382d8b42292c1cb6ebb09213a2fc82e408ccdcff0207d32dc99257d553ffac95b918b028a79c08a7fa7fc7bfbc396f7aa9a0b2b770abc3a93f19ed5  0002-fix-pythondata-cpu-blackparrot-python3.patch
19e48ff191e6755eca198486b516195128b26317146fbd2e854c0f513e49341c014a8d06284a2fda31e04e03946afe135059beb7d3fa358d123e397578336851  pythondata-cpu-blackparrot-2023.12.tar.gz
a8bc38152811f62511389e77b131cb3aa2c6b9c518bfe40406e682682489d91964099dbc50bcff546d530bafed5c8f74130c70428d4db5d1b80ee7494d8b6863  pythondata-cpu-cv32e40p-2023.12.tar.gz
7514804727aabb8a68a3dc9950e0893ccdadeed8d49a7f2b315b737ebcac74bee594b2c1eec06256f70db980f08de930ed0bd4c4c04e030dc64e3141e2399125  pythondata-cpu-cv32e41p-2023.12.tar.gz
b594c60b6ff761f716878eb8652db35e54bee0b2c49424fd06fd0df029a2b3a51f544b4508252b2ac3f7f0e8b10e12f6d270e8d77529f3667d3988305bc94f51  pythondata-cpu-cva5-2023.12.tar.gz
fdc11249059857edd72a321c274a167e8b70848916c8ba54d271a564eb8af507671cee1dd37aa25534c437cbf09c41c5667c82f9440cc74c3a193dea987fe7ed  pythondata-cpu-cva6-2023.12.tar.gz
90254ba5506663d37d18fb099cbd9de91703be7db4972346c39bd828ede109b745904d748a47717a022a2d5a86b71400b0741d9acd7d30dc569736b3976833c7  pythondata-cpu-ibex-2023.12.tar.gz
e37da7c510485160f9686867a301ea182145ce1d6c3f53c4b69fa47ccf040c8bf6696012672d1237a469280010f1f11d73eb5dbe1d6a0adf6318d6c98fc93e69  pythondata-cpu-lm32-2020.04.tar.gz
61b4576f0de33c2a86ff7ade4e10d7c2e2b65771148468710448206d2f022ea3a89a467e99dfd467843fb15d577b0e2e792c4557120d41a9a71e3f1e313b54f3  pythondata-cpu-marocchino-2023.12.tar.gz
c576b9582d9f51de90f398a0ec9011e9423524f401ddad3c54a658adc0b1f19899187586ca6d77c17a62cfbb5cb632a3ff41b8f5ce89868e19b9f17740d6a368  pythondata-cpu-microwatt-2023.12.tar.gz
b19fd16fc2d24579d08b854b9d4a9dbadf5fcee7725f5ba4cc705bbf1ddc8b2951ee6dc2b041cb4550a9ab1618198f2a6e29b932543d380a281aa9faf3c93477  pythondata-cpu-minerva-2023.12.tar.gz
fc7e4fdba394831bd1dce9a47a33cf91f051b04108af9dca8dc57f1a9221d53c16692ef4117a7cca485fdc9684848e31ca9311be73088b8c1108e25fed6e9df4  pythondata-cpu-mor1kx-2020.04.tar.gz
0595dcb4d66ba5f7b865643660cd128efc12b966d4cc336e879239ae73096542063c0886982fcd6afc3df7d0ac6cf9e9bf0b51dae848abe0544093a086ca5157  pythondata-cpu-naxriscv-2023.12.tar.gz
2087236196f9797d4b5746a30634e5d4a535b065c780b85102d7d97b858e30d615d5c2868b61ac8fbb71e671bf2b414eabf2ad6b8a39b34f64f4bde0c907cfb1  pythondata-cpu-picorv32-2020.04.tar.gz
7ae6d8b2cb42f0c5c0093dbb35e8f1eb863b1fd49a4cdaa6ed3ff1b1a2e3ae1ac14a8fe13ed6826f9ada72740b2151075a14eb40df4756c2dd6f02712de91e23  pythondata-cpu-rocket-2020.04.tar.gz
187a78c150a63399131ac4815d2670773917ca8e1de6adec9c7fdbdbb2d78900cc72bd1a103349ed318b1fb7ee17a9e6580db31dc03acd31d68e0aa5e56f378b  pythondata-cpu-serv-2020.04.tar.gz
9f4a0ca2cabfe7cefd5b1be1e40b6f02f2fb5edb9d70099d6d95530f5e4476574a3901340dcac606f7a1a31a2ac29764227f01b93f3e912b299718751746078b  pythondata-cpu-vexriscv-2020.04.tar.gz
e01d0593e4d515f6b38a4373f57f03290bcbd70e8e9117b995055461869db03a277d0ad4faffef0f7775acff48e7ee40666968d6aabf2d926cdeff3a190b5a29  pythondata-cpu-vexriscv_smp-2023.12.tar.gz
66ff1bd80665c6beaf2b8b069698461cf836317a3de1fd06f5959b660e9b2ffefe057c86813c9ca2749ff381ab50a6519f9717eece117b569fb29b84d07163ff  pythondata-misc-tapcfg-2020.04.tar.gz
3e70d5e5941654588a353b866224b4e8b14324d4e771c2d43918ed4749e9c14d1e9229d6fa2f62fb7b58b2964f5d3f5d2f0fee6b14fd3862cfa211a3b93b89bb  pythondata-software-compiler_rt-2020.04.tar.gz
953968a0ba0df4207c04bec43de3ad4b599b0d63182c8ae8da4f254768aacf4a8a8e0a935cdc2aebc23c99494e1d3736b78f1d7413c1f94da3b016036a7f4b0a  pythondata-software-picolibc-2023.12.tar.gz
b9cb43acd169e8e05560ee5d90326a95426e26f8f837c21d3965770f8ed5fdb4352729d4f8320d8070e57fe36e3276d3b04df90498e874d6a6c3fab61fa85829  valentyusb-2023.12.tar.gz
"
